In [68]:
from sympy import *
%matplotlib inline
init_printing()
sigma, sigma1, p, mu, mu1, x, a, b = symbols('sigma, sigma1, p, mu, mu1, x, a, b')
sigma2, mu2 = symbols('sigma2, mu2')
pdf = p * (1 / sigma1) * (exp((-1/2)*((x-mu1)/sigma1)**2)/sqrt(2* pi)) + (1-p) * (1/(b-a))
pdf
Out[68]:
Let's compute the CDF:
In [64]:
cdf = integrate(pdf,x)
cdf = simplify(cdf)
cdf
Out[64]:
Now lets define mu and solve for mu1:
In [70]:
mu_simpli = p * mu1 + (1-p) * ((1/2) * (a+b))
mu1 = solve(Eq(mu_simpli, mu), mu1)
mu1
Out[70]:
In [69]:
sigma_simpli = p * ( (mu1**2) + (sigma1**2)) + (1-p) * ((mu2**2) + (sigma2**2))
sigma1 = solve(Eq(sigma_simpli, sigma), sigma1)
sigma1
Out[69]:
Now we can calculate sigma1 and mu1 with the simplified versions of mu and sigma
In [13]:
sigma1 = solve(Eq(sigma_simpli, sigma), sigma1)
mu1 = solve(Eq(mu_simpli, mu), mu1)
othersigma = sigma1 = ( sigma / p) ** (1 / sqrt(2))/2
sigma1, othersigma
Out[13]:
Let's try it with a mixture of two gaussians:
In [18]:
pdf = p * (1 / sigma1) * (exp((-1/2)*((x-mu1)/sigma1)**2)/sqrt(2* pi)) + (1-p) * (1 / sigma2) * (exp((-1/2)*((x-mu2)/sigma2)**2)/sqrt(2* pi))
pdf
Out[18]:
In [19]:
cdf = integrate(pdf,x)
In [22]:
cdf = simplify(cdf)
cdf
Out[22]:
In [40]:
f = pdf
In [41]:
mu2 = (1/2) * (a+b)
In [42]:
sigma2 = (1/12)*(b-a)**2
In [36]:
delta1 = mu1 - mu
delta2 = mu2 - mu
In [48]:
mean = p* mu1 + (1-p)*mu2
mu1 = mean
In [49]:
variance_simpli = p * (mu1**2 + sigma1**2) + (1-p) * (mu2**2 + sigma2**2)
variance_simpli
In [45]:
sigma1 = solve(Eq(variance_simpli, sigma), sigma1)
In [47]:
sigma1, mu1
Out[47]:
In [ ]: